package com.hankol.material.adapter

import android.widget.ImageView
import android.widget.ProgressBar
import androidx.core.content.ContextCompat
import com.baselibrary.bean.download.DownloadInfo
import com.baselibrary.bean.download.ShowUIUtils
import com.baselibrary.db.DBManager
import com.baselibrary.utils.GlideUtils
import com.baselibrary.utils.HKToastUtils
import com.baselibrary.utils.SpUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.hankol.material.R
import com.hankol.material.limit.DownloadLimitManager


/**下载中 列表
 * @author: merbng
 * @time: 2021/10/15 10:33
 * @description:
 */
class TabDownloadingAdapter : BaseQuickAdapter<DownloadInfo, BaseViewHolder>(R.layout.item_tab_downloading) {
    /**
     * 更新下载进度
     * @param info
     */
    fun updateProgress(info: DownloadInfo) {
        for (i in data.indices) {
            if (data.get(i).getUrl().equals(info.url)) {
                data.set(i, info)
                notifyItemChanged(i)
                break
            }
        }
    }

    val userIds: Int by lazy { SpUtils.getInstance().userId }
    override fun convert(helper: BaseViewHolder, info: DownloadInfo) {
        GlideUtils.loadImg(mContext, R.drawable.ic_hankol_101, R.drawable.ic_hankol_101, info.fileIcon, helper.getView(R.id.img_cover))
        helper.setText(R.id.tv_name, "${info.fileName}").setGone(R.id.img_video_play, info.type == 1)
        val imgStartPause = helper.getView<ImageView>(R.id.img_start_pause)
        GlideUtils.loadImg(mContext, if (DownloadInfo.DOWNLOAD.equals(info.downloadStatus) && !DownloadLimitManager.getInstance().getWaitUrl(info.url)) R.drawable.ic_download_start else R.drawable.ic_download_pause, imgStartPause)

        var stateString = ShowUIUtils.byteToString(info.progress.toDouble())
        if (info.total > 0) {
            stateString += "/${ShowUIUtils.byteToString(info.total.toDouble())}"
        }
        if (info.progress.minus(2000)>info.total){
            helper.setText(R.id.tv_speed, "${ShowUIUtils.byteToString((info.total.minus(2000)).toDouble())}/${ShowUIUtils.byteToString(info.total.toDouble())}")
        }else {
            helper.setText(R.id.tv_speed, stateString)
        }
//        helper.setText(R.id.tv_speed, stateString)

        val progress = helper.getView<ProgressBar>(R.id.progress)

        if (DownloadInfo.DOWNLOAD_CANCEL.equals(info.getDownloadStatus())) {
//            progress.setProgress(0)
        } else if (DownloadInfo.DOWNLOAD_OVER.equals(info.getDownloadStatus())) {
            progress.setProgress(progress.getMax())
        } else {
            if (info.getTotal() == 0L) {
                progress.setProgress(0)
            } else {
                val d = info.getProgress() * progress.getMax() / info.getTotal()
//                progress.setProgress(d.toInt())
                if (d.toInt().minus(2000)>progress.getMax()){
                    progress.setProgress(  d.toInt().minus(2000))
                }else {
                    progress.setProgress(d.toInt())
                }
            }
        }
        if (DownloadInfo.DOWNLOAD.equals(info.downloadStatus) && !DownloadLimitManager.getInstance().getWaitUrl(info.url)) {
            progress.progressDrawable = ContextCompat.getDrawable(mContext, R.drawable.bg_progress)
        } else {
            progress.progressDrawable = ContextCompat.getDrawable(mContext, R.drawable.bg_progress_un)
        }
//        if (DownloadInfo.DOWNLOAD.equals(info.downloadStatus)&&!DownloadLimitManager.getInstance().getWaitUrl(info.url)){
//            DownloadLimitManager.getInstance().download(info)
//            //更新数据库下载状态
//            help.updateDownload(info)
//        }
        imgStartPause.setOnClickListener {
            info.userId = "${userIds}"
            if (info.downloadStatus.equals(DownloadInfo.DOWNLOAD)) {//暂停
                info.downloadStatus = DownloadInfo.DOWNLOAD_PAUSE
                //更新数据库下载状态
                DBManager.getInstance().updateDownload(info)
                DownloadLimitManager.getInstance().pauseDownload(info.url)
                progress.progressDrawable = ContextCompat.getDrawable(mContext, R.drawable.bg_progress_un)
                GlideUtils.loadImg(mContext,R.drawable.ic_download_pause, imgStartPause)
//                notifyDataSetChanged()
            } else {//继续
                if (DownloadLimitManager.getInstance().getWaitUrl(info.url)) {
                    HKToastUtils.showToast("最多同时下载5个")
                    return@setOnClickListener
                }
                info.downloadStatus = DownloadInfo.DOWNLOAD
                //更新数据库下载状态
                DBManager.getInstance().updateDownload(info)
                DownloadLimitManager.getInstance().download(info)
                progress.progressDrawable = ContextCompat.getDrawable(mContext, R.drawable.bg_progress)
                GlideUtils.loadImg(mContext,R.drawable.ic_download_start, imgStartPause)
//                notifyDataSetChanged()
            }
        }
        helper.addOnClickListener(R.id.img_del)
    }
}